package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.comm.TreeNode;
import com.erp.pojo.Dept;
import com.erp.pojo.Goods;
import com.erp.service.*;
import com.erp.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("dept")
public class DeptController {
    @Autowired
    private OutportService outportService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private InportService inportService;
    @Autowired
    private DeptService deptService;

    //http://localhost:8080/outport/loadAllUutport:page=1&l1m1t=1U
    @RequestMapping("loadAllDept")
    public DataAndView loadAllDept(DeptVo deptVo) {

    IPage<Dept> page = new Page<Dept>(deptVo.getPage(),deptVo.getLimit());

    QueryWrapper<Dept> qw = new QueryWrapper<>();
    //title
        if(deptVo.getTitle() != null) {
            qw.like("title", deptVo.getTitle());
            //like eqgt lt
        }
        if(deptVo.getAddress()!= null) {
            qw.like("address", deptVo.getAddress());
        }
        if(deptVo.getRemark()!= null){
            qw.like("remark",deptVo.getRemark());
        }

    deptService.page(page,qw);

    return new DataAndView(page.getRecords());
    }

    //http://localhost:8080/dept/loadDeptManagerleftTreeJson
    @RequestMapping("loadDeptManagerLeftTreeJson")
    public DataAndView loadDeptManagerLeftTreeJson() {

        //查询所有部门信息
        List<Dept> lists = deptService.list();
        List<TreeNode> davs = new ArrayList<>();

        for (Dept dept : lists) {
            boolean spread = dept.getOpen() == 0 ? false : true;
            TreeNode tn = new TreeNode(dept.getId(), dept.getTitle(), spread, "0", dept.getPid());
            davs.add(tn);
        }
        return new DataAndView(davs);
    }


    //添加
    //http://1ocalhost:8080/dept/loadDeptMaxOrderMm
    //获取最大值maxOrderNum +1
    @RequestMapping("loadDeptMaxOrderNum")
    public Map<String,Object> loadDeptMaxOrderNum(){

    Integer maxOrderNum = null;
    QueryWrapper<Dept> qw = new QueryWrapper();
    qw.orderByDesc("ordernum");

    List<Dept> lists = deptService.list(qw);
    maxOrderNum = lists.get(0).getOrdernum();

    Map map= new HashMap<String,Object>();

    if(lists.size()>0){
        map.put("value",maxOrderNum +1);
    } else{
        map.put("value",1);
    }
    return map;
    }

    // http://1ocalhost:8080/dept/addDept
    @RequestMapping("addDept")
    public ResultCode addDept(DeptVo deptVo){
        deptVo.setId(null);
        deptVo.setCreatetime(LocalDateTime.now());
        try{
            boolean bool = deptService.save(deptVo);
            if(bool){
                return ResultCode.SUCCESS_ADD;
            }else {
                return ResultCode.FAIL_ADD;
            }
        }catch (Exception e){
            return ResultCode.FAIL_ADD;
        }
    }

    // 删除http://localhost:8080/goods/deleteGoods

}
